package com.xinyue.game.gateway.server;

import com.xinyue.game.gateway.common.GatewayMessageWrapper;
import com.xinyue.game.gateway.common.GatewayResponseUtil;
import com.xinyue.game.gateway.common.HandleMessageID;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class GatewayHeartbeatHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof GatewayMessageWrapper) {
            GatewayMessageWrapper gatewayMessageWrapper = (GatewayMessageWrapper) msg;
            if (gatewayMessageWrapper.getHeader().getRequestUri().equals(HandleMessageID.HEART_BEAT_URI)) {
                // 收到心跳消息
                ByteBuf responseByteBuf = GatewayResponseUtil.getResponseByteBuf(gatewayMessageWrapper, 0, null);
                ctx.writeAndFlush(responseByteBuf);
            } else {
                ctx.fireChannelRead(msg);
            }
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}
